<?php
namespace App\Http\Controllers\v1;

use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
use App\Http\Controllers\ToolController;
use Illuminate\Http\Request;


class VisaController extends Controller
{
    protected $app_id ='402219';
    protected $app_secret ='3H6axIgvjBqMimZlJiQaylRh';
    protected $customer_id ='EB97A59054032D5073C20740D4EFCB9F';
	
	//修改合同文件内容
	public function UpdateVisaFile($request)
	{
		if(!CheckController::CheckUser($request))
        {
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','ContractFileContent']))
        {
            return RequestBackController::Structure(502);
        }
		$temp=DB::table('yl_visa_contract')->where('case_id',$pos['CaseId'])->first();
		if(!empty($temp))
			DB::table('yl_visa_contract')->where('case_id',$pos['CaseId'])->update(['contract_file_content'=>json_encode($pos['ContractFileContent'])]);
		else
			DB::table('yl_visa_contract')->insert(['contract_file_content'=>json_encode($pos['ContractFileContent']),'case_id'=>$pos['CaseId']]);
		return RequestBackController::Structure(101);
	}
	//查询合同文件
	public function VisaFile($request)
	{
		if(!CheckController::CheckUser($request))
        {
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId']))
        {
            return RequestBackController::Structure(502);
        }
		$res=DB::table('yl_visa_contract')->select('contract_file_content as ContractFileContent','contract_file_path as ContractFilePath')->where('case_id',$pos['CaseId'])->first();
		if(!empty($res->ContractFileContent))
		{
			$res->ContractFileContent=json_decode($res->ContractFileContent);
		}
		if(!isset($res->ContractFileContent))
		{
			$res['ContractFileContent']=null;
			$res['ContractFilePath']=null;
		}
		return RequestBackController::Structure(101,$res);
	}
	//客户对合同文件手动签章
	public function LitigantSignature($request)
	{
		if(!CheckController::CheckUser($request))
        {
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','LitigantId',]))
        {
            return RequestBackController::Structure(502);
        }
		$res=DB::table('yl_visa_contract as v')
		->join("yl_case as c",function($join){
			$join->on('c.case_id','=','v.case_id');
		})
		->select('c.case_type_id','c.case_class_id','v.contract_file_content','v.contract_file_path','v.contract_id')
		->where('v.case_id',$pos['CaseId'])->first();
		if(empty($res))
		{
			return RequestBackController::Structure(504);
		}
		if(empty($res->contract_file_path))
		{
			$res->contract_file_path=$this->Signature($pos,$res);
		}
		$ReturnUrl=$request->header('Referer')?$request->header('Referer'):'';
		if($res->contract_file_path!='0')
		{
			$yl_litigants=DB::table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->first();
			$_time=time();
			$transaction_id=DB::table('yl_visa_transaction')->insertGetId(['transaction_create_time'=>$_time]);
			$transaction='transaction'.$transaction_id;
			$res=$this->VisaManual($yl_litigants->customer_id,$res->contract_id,$transaction,'案件'.$pos['CaseId'].'的合同文件',$yl_litigants->litigant_name,$ReturnUrl);
			return RequestBackController::Structure(101,$res);
		}
		else
		{
			return RequestBackController::Structure(508);
		}		
	}
	//合同文件签章
	public function CaseSignature($request)
	{
		if(!CheckController::CheckUser($request))
        {
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId']))
        {
            return RequestBackController::Structure(502);
        }
		$res=DB::table('yl_visa_contract as v')
		->join("yl_case as c",function($join){
			$join->on('c.case_id','=','v.case_id');
		})
		->select('c.case_type_id','c.case_class_id','v.contract_file_content','v.contract_file_path')
		->where('v.case_id',$pos['CaseId'])->first();
		if(empty($res))
		{
			return RequestBackController::Structure(504);
		}
		if(empty($res->contract_file_path))
		{
			$ret=$this->Signature($pos,$res);
		}
		else{
			$ret=$res->contract_file_path;
		}
		if($ret!='0')
		{
			return RequestBackController::Structure(101,$ret);
		}
		return RequestBackController::Structure(508);			
	}
	protected function Signature($pos,$res)
	{
		$file_path=$this->WordCreate($res->case_type_id,$res->case_class_id,$res->contract_file_content,$pos['CaseId']);
		$PDF_temp_path=$this->WordToPDF(public_path($file_path));
		$_time=time();
		$transaction_id=DB::table('yl_visa_transaction')->insertGetId(['transaction_create_time'=>$_time]);
		$transaction='transaction'.$transaction_id;
		$contract_id='approval_'.$_time.$pos['CaseId'];
		$upload_res=$this->Upload($PDF_temp_path,'案件'.$pos['CaseId'].'的合同文件',$contract_id);
		if($upload_res['result']=='success')
		{
			$VisaRes=$this->VisaAuto($transaction,$contract_id,'案件'.$pos['CaseId'].'的合同文件','乙方：');
			if(isset($VisaRes['code'])&&$VisaRes['code']==1000)
			{
				$output_name='CaseSignature/'.$_time.'_'.$pos['CaseId'].'.pdf';
				$this->Download($contract_id,$output_name);
				DB::table('yl_visa_contract')->where('case_id',$pos['CaseId'])->update(['contract_file_path'=>$output_name,'contract_id'=>$contract_id]);
				return $output_name;
			}
		}
		else
			return 0;
	}
	//用户实名认证
	public function LitigantVisa($request)
	{
		if(!CheckController::CheckUser($request))
        {
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['LitigantId']))
        {
            return RequestBackController::Structure(502);
        }
		$Litigant=DB::table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->first();
		$Litigant->litigant_type_id!=1?$litigant_type=2:$litigant_type=1;
		if(empty($Litigant->customer_id))
		{
			$register=$this->EnrollLitigant($litigant_type,$Litigant->litigant_id);
			if($register['msg']=='success')
			{
				DB::table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->update(['customer_id'=>$register['data']]);
			}
		}
		if($Litigant->authentication!=1)
		{
			if(!empty($Litigant->customer_id))
			{
				$customer_id=$Litigant->customer_id;
			}
			else
			{
				$customer_id=$register['data'];
			}
			$ReturnUrl=$request->header('Referer')?$request->header('Referer'):'';
			if($litigant_type==1)
				$verify=$this->PersonAuthentication($customer_id,$Litigant->litigant_id_card,$Litigant->litigant_name,$ReturnUrl);
			else
				$verify=$this->CompanyAuthentication($Litigant->litigant_name,$Litigant->litigant_id_card,$Litigant->contact_person,$Litigant->contact_person_mobile,$customer_id,$ReturnUrl);
			if($verify['msg']=='success')
			{
				DB::table('yl_litigants')->where('litigant_id',$pos['LitigantId'])->update(['authentication_url'=>$verify['data']['url'],'authentication_transaction_no'=>$verify['data']['transactionNo']]);
			}
			return  RequestBackController::Structure(101,base64_decode($verify['data']['url']));
		}
		else
		{
			return  RequestBackController::Structure(602);
		}
		//{"code":1,"data":"3D4F66CECDCBA82C5AC6E9C82BA68B73","msg":"success"}
	}
	
	//用户认证状态列表
	public function VisaLitigantState($request)
	{
		if(!CheckController::CheckUser($request))
        {
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId']))
        {
            return RequestBackController::Structure(502);
        }
		$yl_litigants=DB::table('yl_litigants')->select('litigant_id as LitigantId','litigant_name as LitigantName','litigant_id_card as LitigantIdCard','customer_id as CustomerId','authentication as Authentication')
					->where('litigation_status_id',1)
					->where('case_id',$pos['CaseId'])
					->get()
					->map(function ($val){
						$val = (array)$val;
						return $val;
						})
					->keyBy(function($item)
                    {
                        return $item['LitigantId'];
                    })
					->toArray();
		$yl_visa_litigant=DB::table('yl_visa_litigant')->select('visa_litigant_id as VisaLitigantId','litigant_id as LitigantId')
					->where('case_id',$pos['CaseId'])
					->get()
					->map(function ($val){
						$val = (array)$val;
						return $val;
						})
					->keyBy(function($item)
                    {
                        return $item['LitigantId'];
                    })
					->toArray();
		foreach($yl_litigants as $k=>$v)
		{
			$yl_litigants[$k]['Visa']=0;
			if(isset($yl_visa_litigant[$k]))
			{
				$yl_litigants[$k]['Visa']=1;
			}
		}
		sort($yl_litigants);
		return RequestBackController::Structure(101,$yl_litigants);
	}
	
	public function ApprovalVisa($ApprovalId)
	{
		$res=DB::table('yl_approval')->select('approval_content')->where('approval_id',$ApprovalId)->first();
		if(empty($res))
		{
			return 0;
		}
		$res=json_decode($res->approval_content,true);
		foreach($res as $key=>$val)
		{
			if(isset($val['content']['seal']) && $val['content']['seal']==1 && isset($val['content']['filesSrc']) && !empty($val['content']['filesSrc']))
			{
				$file_temp=pathinfo($val['content']['filesSrc']);
				if($file_temp['extension']=='docx' ||$file_temp['extension']=='doc')
				{
					$PDF_temp_path=$this->WordToPDF(public_path($val['content']['filesSrc']));
					$_time=time();
					$transaction_id=DB::table('yl_visa_transaction')->insertGetId(['transaction_create_time'=>$_time]);
					$transaction='transaction'.$transaction_id;
					$contract_id='approval_'.$_time.$ApprovalId;
					$upload_res=$this->Upload($PDF_temp_path,$val['content']['fileName'],$contract_id);
					if($upload_res['result']=='success')
					{
						$VisaRes=$this->VisaAuto($transaction,$contract_id,$val['content']['fileName'],'律所盖章');
						if(isset($VisaRes['code'])&&$VisaRes['code']==1000)
						{
							$output_name='VisaFile/'.md5($_time.rand(0,9).rand(0,9).rand(0,9)).'.pdf';
							$this->Download($contract_id,$output_name);
							$res[$key]['content']['filesSrc']=$output_name;
						}
					}
				}
			}
		}
		$res=json_encode($res);
		DB::table('yl_approval')->where('approval_id',$ApprovalId)->update(['approval_content'=>$res]);
	}
	
	//word文件从模板创建
	protected function WordCreate($type_id,$class_id,$content,$case_id)
	{
		if($type_id==1)
		{
			if($class_id==1)
				$file=new \PhpOffice\PhpWord\TemplateProcessor('template/minshi.docx');
			elseif($class_id==2)
				$file=new \PhpOffice\PhpWord\TemplateProcessor('template/xingzheng.docx');
			else
				$file=new \PhpOffice\PhpWord\TemplateProcessor('template/xingshi.docx');
		}
		elseif($type_id==2)
			$file=new \PhpOffice\PhpWord\TemplateProcessor('template/changnian.docx');
		else
			$file=new \PhpOffice\PhpWord\TemplateProcessor('template/zhuanxiang.docx');
		$content=json_decode($content,true);
		foreach($content as $k=>$v)
		{
			$key=$k+1;
			$file->setValue('text'.$key, str_replace('&lt;br/&gt;',"<w:br/>",htmlspecialchars($v)));
		}
		empty($class_id)?$class_id=0:null;
		$path='CaseSignature/'.$type_id.'_'.$class_id.'_'.$case_id.'.docx';
		$file->saveAs($path);
		return $path;
	}
    //word转pdf
    protected function WordToPDF($file_path)
    {
        $java_path='/home/wwwroot/oa-server.yunlianlaw.com/public/jodconverter-2.2.2';
        $output_url1='/home/wwwroot/oa-server.yunlianlaw.com/public/Temp/'.md5(time().rand(0,9).rand(0,9)).'.pdf';
        $input_url1 = $file_path;
		
        $command ='java -jar '.$java_path.'/lib/jodconverter-cli-2.2.2.jar '.$input_url1.' '.$output_url1;
        exec($command,$log,$status);
		return $output_url1;
    }
    //上传文件
    protected function Upload($file_path,$file_name,$contract_id)
    {
        $action='https://testapi.fadada.com:8443/api/uploaddocs.api';
        $path=$file_path;
        $content_arr=[
            'contract_id'=>$contract_id
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort('',$content_arr,$_time),
            'contract_id'=>$contract_id,
            'doc_title'=>$file_name,
            'file'=>new \CURLFile($path),
            'doc_type'=>'.pdf'
        );
        $output=$this->Curl($action,$data);
        return json_decode($output,true);
    }

    //注册用户
    protected function EnrollLitigant($litigant_type,$litigant_id)
    {
        $open_id='User_'.$litigant_id;
        $action='https://testapi.fadada.com:8443/api/account_register.api';
        $content_arr=[
            'account_type'=>$litigant_type,
            'open_id'=>$open_id
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort('',$content_arr,$_time),
            'account_type'=>$litigant_type,
            'open_id'=>$open_id,
        );
        $output=$this->Curl($action,$data);
		return json_decode($output,true);
		/////{"code":1,"data":"3D4F66CECDCBA82C5AC6E9C82BA68B73","msg":"success"}{"code":1,"data":"3D4F66CECDCBA82C5AC6E9C82BA68B73","msg":"success"}
        /////3D4F66CECDCBA82C5AC6E9C82BA68B73
    }

	//企业实名认证
	protected function CompanyAuthentication($litigant_name,$customer_ident_no,$contact_person,$contact_person_mobile,$customer_id,$ReturnUrl)
	{
		$action='https://testapi.fadada.com:8443/api/get_company_verify_url.api';
		
        $content_arr=[
			'agent_info'=>json_encode([
				'agent_id'=>'',
				'agent_id_front_path'=>'', 
				'agent_mobile'=>'',
				'agent_name'=>'',
			]),
			'bank_info'=>json_encode([
				'bank_id'=>'',
				'bank_name'=>'',
				'subbranch_name'=>''
			]),
			'cert_flag'=>0,
			'company_info'=>json_encode([
				'company_name'=>$litigant_name,
				'credit_image_path'=>'',
				'credit_no'=>$customer_ident_no
			]),
			'company_principal_type'=>'1',
            'customer_id'=>$customer_id,
            'legal_info'=>json_encode([
				'legal_id'=>'',
				'legal_id_front_path'=>'',
				'legal_name'=>$contact_person,
				'legal_mobile'=>$contact_person_mobile
			]),
			'notify_url'=>'http://oa-server.yunlianlaw.cn/LitigantVisaReturn',
			'page_modify'=>'2',
			'result_type'=>2,
			'return_url'=>$ReturnUrl,
			'verified_way'=>0
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort('',$content_arr,$_time),
			'agent_info'=>json_encode([
				'agent_id'=>'',
				'agent_id_front_path'=>'', 
				'agent_mobile'=>'',
				'agent_name'=>'',
			]),
			'bank_info'=>json_encode([
				'bank_id'=>'',
				'bank_name'=>'',
				'subbranch_name'=>''
			]),
			'cert_flag'=>0,
			'company_info'=>json_encode([
				'company_name'=>$litigant_name,
				'credit_image_path'=>'',
				'credit_no'=>$customer_ident_no
			]),
			'company_principal_type'=>'1',
            'customer_id'=>$customer_id,
            'legal_info'=>json_encode([
				'legal_id'=>'',
				'legal_id_front_path'=>'',
				'legal_name'=>$contact_person,
				'legal_mobile'=>$contact_person_mobile
			]),
			'notify_url'=>'http://oa-server.yunlianlaw.cn/LitigantVisaReturn',
			'page_modify'=>'2',
			'result_type'=>2,
			'return_url'=>$ReturnUrl,
			'verified_way'=>0
        );
        $output=$this->Curl($action,$data);
		return json_decode($output,true);
	}
    //个人实名认证
    protected function PersonAuthentication($customer_id,$customer_ident_no,$customer_name,$ReturnUrl)
    {
        //$customer_id='3D4F66CECDCBA82C5AC6E9C82BA68B73';
		//$customer_ident_no///证件号码
		//$customer_name////姓名
		
        $action='https://testapi.fadada.com:8443/api/get_person_verify_url.api';
		
        $content_arr=[
            'customer_id'=>$customer_id,
            'cert_flag'=>'1',
            'customer_ident_no'=>$customer_ident_no,
            'customer_ident_type'=>0,
            'customer_name'=>$customer_name,
            'ident_front_path'=>'',
            'notify_url'=>'http://oa-server.yunlianlaw.cn/LitigantVisaReturn',
            'page_modify'=>'2',
            'result_type'=>'2',
            'return_url'=>$ReturnUrl,
            'verified_way'=>'2'
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort('',$content_arr,$_time),
            'customer_id'=>$customer_id,
            'cert_flag'=>'1',
            'customer_ident_no'=>$customer_ident_no,
            'customer_ident_type'=>0,
            'customer_name'=>$customer_name,
            'ident_front_path'=>'',
            'notify_url'=>'http://oa-server.yunlianlaw.cn/LitigantVisaReturn',
            'page_modify'=>'2',
            'result_type'=>'2',
            'return_url'=>$ReturnUrl,
            'verified_way'=>'2'
        );
        $output=$this->Curl($action,$data);
		return json_decode($output,true);
        //{"code":1,
        //"data":{"transactionNo":"7b940dfb88b042f4b95143f10e05009f",
        //"url":"aHR0cHM6Ly9yZWFsbmFtZXZlcmlmeS10ZXN0LmZhZGFkYS5jb20vZmRkQXV0aGVudGljYXRpb25TZXJ2aWNlL3YxL2FwaS9zeW5zQXV0aGVudGljYXRpb24uYWN0aW9uP3RyYW5zYWN0aW9uX25vPUQxMzAzRDU3MzhDMDBEQUYxRjkyNEQ2MkE0MTQxRDU4REI5NEVBOUE4RTcwRDU1OTlDNUI3QkYyQTMzMUNGQjRDMzM3NTFGMDM4MUMyQ0UyJnNpZ249TmpReE16STBNalpDUTBVek9FUkRRVVJHT0RORVJFWkdORUUzUVVWRU5EazVOMFUzTlRWQ01BPT0mYXBwX2lkPTQwMjIxOSZ0aW1lc3RhbXA9MTU2MjY1NDM0ODk2NQ=="},
        //"msg":"success"}
    }

    //自动签章
    protected function VisaAuto($transaction_id,$contract_id,$file_name,$sign_keyword)
    {
        $action='https://testapi.fadada.com:8443/api/extsign_auto.api';
		
        $content_arr=[
            'customer_id'=>$this->customer_id
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort($transaction_id,$content_arr,$_time),
            'transaction_id'=>$transaction_id,
            'contract_id'=>$contract_id,
            'customer_id'=>$this->customer_id,
            'client_role'=>1,
            'doc_title'=>$file_name,
            'sign_keyword'=>$sign_keyword
            //'sign_keyword'=>'乙方：'
        );
		
        $output=$this->Curl($action,$data);
        return json_decode($output,true);
		//{"code":"1000",
		//"download_url":"https:\/\/testapi.fadada.com:8443\/api\/\/getdocs.action?app_id=402219&timestamp=20190719153034&v=2.0&msg_digest=OTM1M0IwQzFDMjM0MEEyRDcwQTdDOUNCMTkzMkExMUEwNkVGMzVDMw==&send_app_id=null&transaction_id=transaction19","msg":"文档签署成功","result":"success","viewpdf_url":"https:\/\/testapi.fadada.com:8443\/api\/\/viewdocs.action?app_id=402219&timestamp=20190719153034&v=2.0&msg_digest=OTM1M0IwQzFDMjM0MEEyRDcwQTdDOUNCMTkzMkExMUEwNkVGMzVDMw==&send_app_id=null&transaction_id=transaction19"}
    }
	////
	////
	////
	////
	////
	//客户手动签章
	protected function VisaManual($customer_id,$contract_id,$transaction_id,$doc_title,$sign_keyword,$ReturnUrl)
	{
		$action='https://testapi.fadada.com:8443/api/extsign.api';
		
        $content_arr=[
            'customer_id'=>$customer_id
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort($transaction_id,$content_arr,$_time),
            'transaction_id'=>$transaction_id,
            'contract_id'=>$contract_id,
            'customer_id'=>$customer_id,
            'doc_title'=>$doc_title,
            'sign_keyword'=>$sign_keyword,
			'keyword_strategy'=>'2',
			'notify_url'=>'http://oa-server.yunlianlaw.cn/ManualVisaReturn',
			'return_url'=>$ReturnUrl
            //'sign_keyword'=>'乙方：'
        );
		//print_r($data);exit();
        //$output=$this->Curl($action,$data,1);
		$output=$action.'?';
		$key=0;
		foreach($data as $k=>$v)
		{
			if($key!=0)
			{
				$output.='&';
			}
			$output.=$k.'='.$v;
			$key++;
		}
        return $output;
	}
	//用户签章回调
	public function ManualVisaReturn(Request $request)
	{
		$pos=$request->all();
		if(isset($pos['result_code'])&&isset($pos['contract_id']))
		{
			if($pos['result_code']==3000)
				exit();
		}
		return 1;
	}
	//用户实名认证回调
	public function LitigantVisaReturn(Request $request)
	{
		$pos=$request->all();
		if(isset($pos['status'])&&isset($pos['customerId']))
		{
			if($pos['status']==2)
				DB::table('yl_litigants')->where('litigant_type_id',1)->where('customer_id',$pos['customerId'])->update(['authentication'=>1]);
			if($pos['status']==4)
				DB::table('yl_litigants')->where('litigant_type_id',"<>",1)->where('customer_id',$pos['customerId'])->update(['authentication'=>1]);
		}
		return 1;
	}
	////
	////
	////
	////
	////
	////
	////
	////
	////
    protected function Download($contract_id,$output_name)
    {
        $action='https://testapi.fadada.com:8443/api/downLoadContract.api';

        $content_arr=[
            'contract_id'=>$contract_id
        ];
        $_time=date('YmdHis');
        $data = array(
            'app_id'=>$this->app_id,
            'timestamp'=>$_time,
            'v'=>'2.0',
            'msg_digest'=>$this->ascll_sort('',$content_arr,$_time),
            'contract_id'=>$contract_id
        );
        $output=$this->Curl($action,$data);
        file_put_contents($output_name,$output);
    }

    protected function Curl($action,$data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $action);
		curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  //该curl_setopt可以向header写键值对
        curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    protected function ascll_sort($_arr,$arr,$_time)
    {
        ksort($arr);
        $res_sort='';
        foreach($arr as $k=>$v)
        {
            $res_sort.=$v;
        }
        $res=base64_encode(
            strtoupper(
                sha1(
                    $this->app_id.
                    strtoupper(
                        MD5($_arr.$_time)
                    ).
                    strtoupper(
                        sha1($this->app_secret.$res_sort)
                    )
                )
            )

        );
        return $res;
    }
}